package com.aaa.controller;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.entity.Result;
import com.aaa.service.CountService;
import com.aaa.service.impl.CountServiceImpl;
import com.aaa.util.MyTool;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈
 * @date ：Created in 2020/10/31 14:59
 * @description：统计servlet
 * @modified By：
 * @version: 1.0
 */
@WebServlet(name = "CountServlet",urlPatterns = "/count")
public class CountServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //返回所有的请求参数
        Map<String, String[]> parameterMap = req.getParameterMap();
        //将 Map<String, String[]>转换成 Map<String, String>
        Map<String, String> myMap = new HashMap<>();
        //遍历原始map
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            if (ObjectUtil.isNotEmpty(entry.getValue())) {
                myMap.put(entry.getKey(), entry.getValue()[0]);
            }
        }
        String method = myMap.get("method") + "";
        Result result = new Result();
        CountService countService= new CountServiceImpl();
        switch (method) {

            case "byEmp":
              Map<String, Object> myCount = countService.countByEmp();
                result = MyTool.getSuccessReturn(result);
                result.setData(myCount);
                break;
        }
        String jsonString = JSON.toJSONStringWithDateFormat(result, "yyyy-MM-dd HH:mm:ss");
        resp.getWriter().print(jsonString);
    }
}
